//
//  XWXTextView.m
//  XWXTextView
//
//  Created by mac on 2017/11/21.
//  Copyright © 2017年  - zwhl. All rights reserved.
//

#import "XWXTextView.h"

@interface XWXTextView()
/**
 *  占位文字Label
 */
@property (weak, nonatomic) UILabel *phLabel;

@end

@implementation XWXTextView

- (UILabel *)phLabel {
    if (!_phLabel) {
        UILabel *phLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 7, self.width-8, 35)];
        // 文字自动换行
        phLabel.numberOfLines = 0;
        phLabel.textColor = [UIColor colorWithHexString:@"#CCCCCC"];
        [self addSubview:phLabel];
        _phLabel = phLabel;
    }
    return _phLabel;
}

-(instancetype)initWithCoder:(NSCoder *)coder {
    if (self = [super initWithCoder:coder]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 添加监听器，监听自己的文字改变通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

// 时刻监听文字键盘文字的变化，文字一旦改变便调用setNeedsDisplay方法
- (void)textDidChange {
    // 有文字就隐藏
    self.phLabel.hidden = self.hasText;
}

-(void)dismissKeyBoard {
    [self resignFirstResponder];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    // 确定label的宽度，高度由文字数量自动计算
    CGSize size = CGSizeMake(self.width, MAXFLOAT);
    // 根据文字的字体属性、文字的数量动态计算label的尺寸
    self.phLabel.size = [self.placeholder boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:nil].size;
}

// 占位文字
- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    self.phLabel.text = placeholder;
    // 更新文字尺寸
    [self setNeedsLayout];
}

// 字体
- (void)setFont:(UIFont *)font {
    [super setFont:font];
    self.phLabel.font = font;
    [self setNeedsLayout];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
